using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using Decav.Data;
using Decav.Gatsb.Web.Pages;
using Decav.Gatsb.Web;

public partial class Posts : System.Web.UI.Page
{

    private static Dictionary<string, Type> _adapters = new Dictionary<string, Type>(StringComparer.OrdinalIgnoreCase);

    /// <summary>
    /// Gets the page to show posts for.
    /// </summary>
    public int CurrentPage
    {
        get
        {
            if (Request.QueryString["Page"] == null || Request.QueryString["Page"].Trim().Length == 0)
                return 1;

            int i = 0;
            if (!Int32.TryParse(Request.QueryString["Page"], out i))
                Response.Redirect("Posts.aspx", true);

            return i;
        }
    }

    public string CurrentPostType
    {
        get
        {
            if (Request.QueryString["Type"] == null || Request.QueryString["Type"].Trim().Length == 0)
                return null;    
                
            return Request.QueryString["Type"];
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!_adapters.ContainsKey(CurrentPostType))
            Response.Redirect("Posts.aspx");

        IPostsPageAdapter adapt = (IPostsPageAdapter)Activator.CreateInstance(_adapters[CurrentPostType]);
        PostsPageAdapterResult result = adapt.Execute(40);

        pageHeader.InnerHtml = result.PageHeader;
        GatsbWebApplication.SetPageName(Page, pageHeader.InnerText);
        postRepeater.DataSource = result.Posts.Items;

    }
}
